<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="产品名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="产品名称"></el-input>
    </el-form-item>
    <el-form-item label="描述" prop="content">
      <el-input v-model="dataForm.content" placeholder="描述"></el-input>
    </el-form-item>
    <el-form-item label="现价" prop="price">
      <el-input v-model="dataForm.price" placeholder="现价"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="缩略图" prop="thumbnail">
      <el-input v-model="dataForm.thumbnail" placeholder="缩略图"></el-input>
    </el-form-item>
    <el-form-item label="是否是新品" prop="type">
      <el-input v-model="dataForm.type" placeholder="是否是新品"></el-input>
    </el-form-item>
    <el-form-item label="0下架，1上架" prop="status">
      <el-input v-model="dataForm.status" placeholder="0下架，1上架"></el-input>
    </el-form-item>
    <el-form-item label="商品分类id" prop="categoryId">
      <el-input v-model="dataForm.categoryId" placeholder="商品分类id"></el-input>
    </el-form-item>
    <el-form-item label="排序" prop="sort">
      <el-input v-model="dataForm.sort" placeholder="排序"></el-input>
    </el-form-item>
    <el-form-item label="详情图" prop="detailsPic">
      <el-input v-model="dataForm.detailsPic" placeholder="详情图"></el-input>
    </el-form-item>
    <el-form-item label="轮播图" prop="bannerPic">
      <el-input v-model="dataForm.bannerPic" placeholder="轮播图"></el-input>
    </el-form-item>
    <el-form-item label="参数图" prop="paramPic">
      <el-input v-model="dataForm.paramPic" placeholder="参数图"></el-input>
    </el-form-item>
    <el-form-item label="销售量" prop="salesNum">
      <el-input v-model="dataForm.salesNum" placeholder="销售量"></el-input>
    </el-form-item>
    <el-form-item label="视频链接" prop="videoid">
      <el-input v-model="dataForm.videoid" placeholder="视频链接"></el-input>
    </el-form-item>
    <el-form-item label="0:未删除，1：删除" prop="isDel">
      <el-input v-model="dataForm.isDel" placeholder="0:未删除，1：删除"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          content: '',
          price: '',
          createTime: '',
          thumbnail: '',
          type: '',
          status: '',
          categoryId: '',
          sort: '',
          detailsPic: '',
          bannerPic: '',
          paramPic: '',
          salesNum: '',
          videoid: '',
          isDel: ''
        },
        dataRule: {
          name: [
            { required: true, message: '产品名称不能为空', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '描述不能为空', trigger: 'blur' }
          ],
          price: [
            { required: true, message: '现价不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          thumbnail: [
            { required: true, message: '缩略图不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '是否是新品不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '0下架，1上架不能为空', trigger: 'blur' }
          ],
          categoryId: [
            { required: true, message: '商品分类id不能为空', trigger: 'blur' }
          ],
          sort: [
            { required: true, message: '排序不能为空', trigger: 'blur' }
          ],
          detailsPic: [
            { required: true, message: '详情图不能为空', trigger: 'blur' }
          ],
          bannerPic: [
            { required: true, message: '轮播图不能为空', trigger: 'blur' }
          ],
          paramPic: [
            { required: true, message: '参数图不能为空', trigger: 'blur' }
          ],
          salesNum: [
            { required: true, message: '销售量不能为空', trigger: 'blur' }
          ],
          videoid: [
            { required: true, message: '视频链接不能为空', trigger: 'blur' }
          ],
          isDel: [
            { required: true, message: '0:未删除，1：删除不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sys/goods/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.goods.name
                this.dataForm.content = data.goods.content
                this.dataForm.price = data.goods.price
                this.dataForm.createTime = data.goods.createTime
                this.dataForm.thumbnail = data.goods.thumbnail
                this.dataForm.type = data.goods.type
                this.dataForm.status = data.goods.status
                this.dataForm.categoryId = data.goods.categoryId
                this.dataForm.sort = data.goods.sort
                this.dataForm.detailsPic = data.goods.detailsPic
                this.dataForm.bannerPic = data.goods.bannerPic
                this.dataForm.paramPic = data.goods.paramPic
                this.dataForm.salesNum = data.goods.salesNum
                this.dataForm.videoid = data.goods.videoid
                this.dataForm.isDel = data.goods.isDel
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/goods/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'content': this.dataForm.content,
                'price': this.dataForm.price,
                'createTime': this.dataForm.createTime,
                'thumbnail': this.dataForm.thumbnail,
                'type': this.dataForm.type,
                'status': this.dataForm.status,
                'categoryId': this.dataForm.categoryId,
                'sort': this.dataForm.sort,
                'detailsPic': this.dataForm.detailsPic,
                'bannerPic': this.dataForm.bannerPic,
                'paramPic': this.dataForm.paramPic,
                'salesNum': this.dataForm.salesNum,
                'videoid': this.dataForm.videoid,
                'isDel': this.dataForm.isDel
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
